﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class shop_AddNews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {//获取连接字符串
            string connectionString = ConfigurationManager.ConnectionStrings["keshe2ConnectionString"].ToString();
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();//打开连接
            SqlDataAdapter oda = new SqlDataAdapter("SELECT * FROM 通知", conn);
            DataSet ds = new DataSet();
            oda.Fill(ds, "通知");
            conn.Close();
            DataRow dr = ds.Tables["通知"].NewRow();//新建一个数据行
            string fileAttention = System.IO.Path.GetExtension(fupPicture.PostedFile.FileName);//获取图片文件的扩展名
            DateTime submitDate = System.DateTime.Now;
            string currentDate = submitDate.ToString("yyyyMMddHHmmssFFFF");//格式化日期
            string pictureFileName = "~/image/" + currentDate + fileAttention;
            dr["标题"] = txtTitle.Text;
            dr["内容"] = txtContent.Text;
            dr["时间"] = submitDate.ToString();
            if (fupPicture.HasFile)//判断有没有附件
            {
                dr["图片"] = pictureFileName;
            }
          
            ds.Tables["通知"].Rows.Add(dr);//将数据行添加到表中
            SqlCommandBuilder ocb = new SqlCommandBuilder(oda);
            oda.Update(ds, "通知");//提交更改
            fupPicture.SaveAs(Server.MapPath(pictureFileName));//上传图片
     
            labMsg.Text = "通知发布成功！";
        }
        catch
        {
            labMsg.Text = "通知发布失败！";
        }
    }
}